/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.async;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;

import com.phonebooksharing.android.activities.dashboard.PhotoReaderActivity;
import com.phonebooksharing.android.util.Global;
import com.phonebooksharing.android.util.IOUtilities;
import com.phonebooksharing.android.util.GalleryUtilities;
import com.phonebooksharing.android.util.NetworkHandlerTask;
import com.phonebooksharing.android.widgets.drawable.FastBitmapDrawable;
import com.phonebooksharing.dto.dashboard.DashboardReaderItem;
import com.phonebooksharing.dto.friend.ContactItem;
import com.phonebooksharing.dto.gallery.ImageGalleryItem;

@SuppressWarnings("unchecked")
public class DownloadNetworkImageTask extends NetworkHandlerTask<Object, Object, Intent> {
    private Activity caller;
    private ProgressDialog d;
    private File file;
    
    public DownloadNetworkImageTask(Activity caller) {
        this.caller = caller;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        d = new ProgressDialog(caller);
        d.setTitle("Downloading Photo");
        d.setMessage("Please wait while loading...");
        d.setIndeterminate(true);
        d.setCancelable(true);
        d.show();
    }

    @Override
    protected Intent doInBackground(Object... params) {
        long id = (Long) params[0];
        int w = (Integer) params[1];
        int h = (Integer) params[2];

        String url = String.format("http:" + Global.IMAGE_SERVLET_URL, "gic" /*ac*/, id /*q*/, "" /*c*/, "" /*p*/, "" /*ps*/, "" /*mt*/, w /*w*/, h /*h*/);
        try {
            // TODO: shall we add this to cache?!?!
            // ImageUtilities.addToCache(id, b);

            file = IOUtilities.getExternalFile(System.currentTimeMillis() + ".dat");
            IOUtilities.add2collection(file.getAbsolutePath());
            GalleryUtilities.downloadAndStore(url, file);

        } catch (IOException e) {
            e.printStackTrace();
        }
        
        final Intent i = new Intent(caller, PhotoReaderActivity.class);
        final ImageGalleryItem f;
        if(params[3] instanceof DashboardReaderItem) {
            final DashboardReaderItem<ContactItem> dr = (DashboardReaderItem<ContactItem>)params[3];
            f = (ImageGalleryItem) dr.getItem();

            i.putExtra(PhotoReaderActivity.SENT_WHEN, dr.getWhen());
            i.putExtra(PhotoReaderActivity.DASHBOARD_ID, dr.getId());
            i.putExtra(PhotoReaderActivity.DASHBOARD_READER_ITEM, dr);

        } else {
            f = (ImageGalleryItem)params[3];
            i.putExtra(PhotoReaderActivity.SENT_WHEN, f.when);
            i.putExtra(PhotoReaderActivity.CONTACT_ITEM, f);
        }
        
        i.putExtra(PhotoReaderActivity.PHOTO_TITLE,     f.title);
        i.putExtra(PhotoReaderActivity.PHOTO_ID,        f.photoID);
        i.putExtra(PhotoReaderActivity.PHOTO_WIDTH,     f.width);
        i.putExtra(PhotoReaderActivity.PHOTO_HEIGHT,    f.height);
        i.putExtra(PhotoReaderActivity.PHOTO_LATITUDE,  f.imageLatitude);
        i.putExtra(PhotoReaderActivity.PHOTO_LONGITUDE, f.imageLongitude);

        // common stuff
        FastBitmapDrawable fb = GalleryUtilities.getFromContactCache(f.andID);
        if(fb != null) {
            i.putExtra(PhotoReaderActivity.FRIEND_ICON, fb.getBitmap());
        } else {
            i.putExtra(PhotoReaderActivity.FRIEND_ICON, GalleryUtilities.noPictureBitmap.getBitmap());
        }
        i.putExtra(PhotoReaderActivity.FRIEND_NAME,     f.name);
        i.putExtra(PhotoReaderActivity.FRIEND_STATUS,   f.status);
        i.putExtra(PhotoReaderActivity.FRIEND_LOCATION, f.location);
        i.putExtra(PhotoReaderActivity.FRIEND_ID,       f.andID);

        i.putExtra(PhotoReaderActivity.PHOTO_FILENAME, file.getAbsolutePath());
        
        return i;
    }

    @Override
    protected void onPostExecute(Intent i) {
        super.onPostExecute(i);

        d.dismiss();
        caller.startActivity(i);
    }
}